
#include "kb.h"                 // keybaord event type 
#include "touch_algorithm.h"    // touch event type 
#include "custom_event.h"
#include "gui.h"                // GUI header

#include "views.h"              // include all views 
#include "log.h"                // LOG file 
#define LOG     LOG_INFO0


#include "xapp_clock.h"
#include "xapp_access.h"
#include "xapp_profile.h"

#include "views_util.h"

enum tagBrightness
{
    tagBrightnessDec = 0,
    tagBrightnessInc,
};

// declare function 
GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag );

const static ui_widgets_type_t wglBrightness/*1. modify name*/[] = 
{
    // background
    [0] = uiCanvas( 0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ) ,

    // icon
    [1] = uiPictureBox( IMG_Setting_Item_Icon_075_075_30, 0, SIZE_SCALE(120-25), SIZE_SCALE(10), SIZE_SCALE(50), SIZE_SCALE(50) ),
    // bar
    [2] = uiDigital( IMG_Setting_Brightness_Bar_144_062_06, SIZE_SCALE(73), SIZE_SCALE(130), SIZE_SCALE(96), SIZE_SCALE(41) ),
    // button DEC 
    [3] = uiPictureBox( IMG_Setting_Brightness_Button_065_062_02, 0, SIZE_SCALE(23), SIZE_SCALE(130), SIZE_SCALE(43), SIZE_SCALE(41) ),
    // button INC 
    [4] = uiPictureBox( IMG_Setting_Brightness_Button_065_062_02, 1, SIZE_SCALE(174), SIZE_SCALE(130), SIZE_SCALE(43), SIZE_SCALE(41) ),
};

const static ui_refresh_part_t rectPartBrightness[] = 
{
    [0] = xRECTANGLE( SIZE_SCALE(73), SIZE_SCALE(130), SIZE_SCALE(96), SIZE_SCALE(41) )
};

const static ui_widgets_type_t wglBrightnessTouch[] = 
{
    [0] = uiTouch( 0, SIZE_SCALE(110), SIZE_SCALE(120), SIZE_SCALE(80), tagBrightnessDec ),
    [1] = uiTouch( SIZE_SCALE(120), SIZE_SCALE(110), SIZE_SCALE(120), SIZE_SCALE(80), tagBrightnessInc ),
}; 


GUI_VIEW_DEF( viewSettings_Brightness ) = 
{
    // name 
    .name = "Settings+Brightness",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglBrightness /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT( wglBrightness ), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC
};


/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 0;
    menu->lifetime = 5000; 
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{    

    return GUI_DESTROY_RET_DEFAULT; 
}


/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;

    const profile_system_t* p_profile_sys = xapp_profile_get_system();

    prop.digital.idx = views_util_brightness_level_to_6bars( p_profile_sys->brightness );
    gui_widget_update_prop( &widgets[2], &prop );

    if( IS_REQ_SRC_CREATE( menu->source ) )
    {
        gui_part_t gui_part = GUI_PART_CONFIG(rectPartBrightness, 0 );
        gui_part_create( &gui_part );
    }
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;

    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_CLICK:
        VIEW_CHECK_TOUCH( wglBrightnessTouch, touch->x, touch->y, local_view_event_handler );
        gui_core_lifetime_refresh( menu );        
        break;
                
        case TP_ALGO_EVT_TYPE_SLIDE_R:
        if( p_this_parent != NULL )
        {
            gui_view_create_return( p_this_parent );
        }
        else 
        {
            gui_view_create( &viewAppsList );
        }
        break;
        
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t* kb = (kb_evt_t*)pvParameters;

    switch( kb->type )
    {
        case KB_EVT_CLICK:
        if( p_this_parent != NULL )
        {
            gui_view_create_return( p_this_parent );
        }
        else 
        {
            gui_view_create( &viewAppsList );
        }
        break;
    }
}

/* 界面的自定义输入事件处理

*/
static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    custom_evt_t* custom_event = (custom_evt_t*)pvParameters;

    if( CUSTOM_EVT_TYPE_BRIGHTNESS_UPDATE == custom_event->type )
    {
        gui_update_part();
    }
}

/* 界面的触摸事件
   tag 是生效的控件id, 用户根据生效的tag判断是哪个控件点击了，进行处理
*/
static void local_view_event_handler( uint32_t tag )
{
    switch( tag )
    {
        case tagBrightnessDec:
        xapp_access_BRIGHTNESS_dec( 20 );
        gui_update_part();
        break;

        case tagBrightnessInc:
        xapp_access_BRIGHTNESS_inc( 20 );
        gui_update_part();
        break;
    }
}

